/*
通过工具类Collectors提供了具体的收集方式
    public static <T> Collector toList() : 把元素收集到List集合中
    public static <T> Collector toSet() : 把元素收集到Set集合中
    public static Collector toMap(Function keyMapper , Function valueMapper ) : 把元素收集到Map集合中
 */

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class CollectorDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("林青霞");
        list.add("张曼玉");
        list.add("王祖贤");
        list.add("柳岩");
        //1.得到名字为3个字的流
        Stream<String> listStream = list.stream().filter(s -> s.length() == 3);
        //把使用流操作完毕的数据收集到List集合中并遍历
        List<String> list1 = listStream.collect(Collectors.toList());
        for(String str : list1)
            System.out.print(str + " ");
        System.out.println();

        Set<Integer> set = new HashSet<Integer>();
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);
        set.add(50);
        //2.得到年龄大于25的流
        Set<Integer> set1 = set.stream().filter(i -> i > 25).collect(Collectors.toSet());
        for(Integer i : set1)
            System.out.println(i + " ");
        System.out.println();

        String[] strArray = {"林青霞,30","张曼玉,35","王祖贤,33","柳岩,25" };
        //3.得到字符串中年龄数据大于28的流
        Map<String, Integer> map = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28).collect(
                Collectors.toMap((s -> s.split(",")[0]), s -> Integer.parseInt(s.split(",")[1])));
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for(Map.Entry<String, Integer> me : entries)
            System.out.println(me.getKey() + " , " + me.getValue());
    }
}
